package com.custom.base.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.custom.base.R;


/**
 * 加载中Dialog
 * @author xm
 */
public class LoadingDialog extends AlertDialog {

    private ImageView tips_loading_gif;
    private MyAnimationDrawable animationDrawable;

    public LoadingDialog(Context context) {
        super(context);
        this.setCancelable(false);
    }

    public LoadingDialog(Context context, String message) {
        super(context);
        this.setCancelable(false);
    }

    public LoadingDialog(Context context, int theme, String message) {
        super(context, theme);
        this.setCancelable(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.view_tips_loading);
        Log.e("~~~", "onCreate: " );
        tips_loading_gif = findViewById(R.id.tips_loading_gif);
//        tips_loading_gif.setImageResource(R.drawable.frame_loading);
//        animationDrawable = (AnimationDrawable) tips_loading_gif.getDrawable();
//        if(animationDrawable!=null){
//            animationDrawable.start();
//        }
        animationDrawable = MyAnimationDrawable.getAnimationDrawable().animateRawManuallyFromXML(R.drawable.list_loading, tips_loading_gif,true, new Runnable() {
            @Override
            public void run() {
                Log.e("~~~", "run: start" );
                // 动画开始时回调
            }
        }, new Runnable() {
            @Override
            public void run() {
                Log.e("~~~", "run: stop" );

                // 动画结束时回调
            }
        });
    }

    @Override
    public void cancel() {
        if(animationDrawable!=null){
            animationDrawable.stop();
        }
        try {
            super.cancel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if(animationDrawable!=null){
                animationDrawable.stop();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}